package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.FootprintMapper;
import org.linlinjava.litemall.db.models.Footprint;
import org.linlinjava.litemall.db.service.FootprintService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class FootprintServiceImpl extends ServiceImpl<FootprintMapper, Footprint> implements FootprintService {

    @Override
    public IPage<Footprint> queryByAddTime(Long userId, Integer pageNo, Integer pageSize) {
        QueryWrapper<Footprint> query = new QueryWrapper<>();
        Page<Footprint> page = new Page<>(pageNo, pageSize);
        query.eq("user_Id", userId);
        query.orderByDesc("add_time");
        return baseMapper.selectPage(page, query);
    }

    @Override
    public Footprint findById(Long userId, Integer id) {
        QueryWrapper<Footprint> query = new QueryWrapper<>();
        query.eq("id", id).eq("user_id", userId);
        return this.baseMapper.selectOne(query);
    }

    @Override
    public IPage<Footprint> querySelective(String userId, String goodsId, Integer pageNo, Integer pageSize) {
        Page<Footprint> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Footprint> query = new QueryWrapper<>();
        query.eq(!StringUtils.isEmpty(goodsId), "goods_id", goodsId)
                .eq(!StringUtils.isEmpty(userId), "user_id", userId);

        return baseMapper.selectPage(page, query);
    }
}
